package com.hdoit.ares.logical.policy.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import com.hdoit.ares.logical.acc.model.WithdrawParam;
import com.hdoit.ares.logical.acc.service.IGoldenBeanDetailService;
import com.hdoit.ares.logical.acc.service.IWithdrawParamService;
import com.hdoit.ares.logical.cust.model.Customer;
import com.hdoit.ares.logical.cust.service.ICustomerService;
import com.hdoit.ares.logical.insu.model.FanhuaAccident;
import com.hdoit.ares.logical.insu.model.FanhuaCar;
import com.hdoit.ares.logical.insu.model.ZhonganAccident;
import com.hdoit.ares.logical.insu.model.ZhonganCar;
import com.hdoit.ares.logical.policy.dao.IListPolicyDao;
import com.hdoit.ares.logical.policy.model.ListPolicy;
import com.hdoit.ares.logical.policy.service.IListPolicyService;
import com.hdoit.ares.logical.viewmodel.policy.ListPolicySearch;
import com.hdoit.ares.selfcommon.enums.acc.GoldenBeanType;
import com.hdoit.ares.selfcommon.enums.acti.TaskId;
import com.hdoit.ares.selfcommon.enums.policy.PolicySignsState;
import com.tennetcn.common.dao.base.impl.SuperService;
import com.tennetcn.common.enums.ModelStatus;
import com.tennetcn.common.message.PagerModel;
import com.tennetcn.common.utils.CommonUtils;
import com.xiaoleilu.hutool.log.dialect.log4j2.Log4j2LogFactory;
/**
 * @author      caochengmeng
 * @email       
 * @createtime  2018年01月18日 00:53:22
 * @comment
 */
@Component
public class ListPolicyServiceImpl extends SuperService<ListPolicy> implements IListPolicyService{
	
	private IGoldenBeanDetailService goldenBeanDetailService;
	private ICustomerService customerService;
	private IWithdrawParamService withdrawParamService;

	public IGoldenBeanDetailService getGoldenBeanDetailService() {
		return goldenBeanDetailService;
	}
	@Resource
	public void setGoldenBeanDetailService(IGoldenBeanDetailService goldenBeanDetailService) {
		this.goldenBeanDetailService = goldenBeanDetailService;
	}
	public ICustomerService getCustomerService() {
		return customerService;
	}
	@Resource
	public void setCustomerService(ICustomerService customerService) {
		this.customerService = customerService;
	}
	public IWithdrawParamService getWithdrawParamService() {
		return withdrawParamService;
	}
	@Resource
	public void setWithdrawParamService(IWithdrawParamService withdrawParamService) {
		this.withdrawParamService = withdrawParamService;
	}

	private IListPolicyDao listPolicyDao;
	public IListPolicyDao getListPolicyDao() {
		return listPolicyDao;
	}
	@Resource
	public void setListPolicyDao(IListPolicyDao listPolicyDao) {
		this.listPolicyDao = listPolicyDao;
	}

	@Override
	public List<ListPolicy> queryAllAccidentPolicy(String customerId) {
		return listPolicyDao.queryAllAccidentPolicy(customerId);
	}

	@Override
	public List<ListPolicy> queryAllCarPolicy(String customerId) {
		return listPolicyDao.queryAllCarPolicy(customerId);
	}
	@Override
	public ListPolicy queryFanhuaAccident(String customerId, String oid) {
		return listPolicyDao.queryFanhuaAccident(customerId, oid);
	}
	@Override
	public ListPolicy queryZhonganAccident(String customerId, String oid) {
		return listPolicyDao.queryZhonganAccident(customerId, oid);
	}
	@Override
	public ListPolicy queryFanhuaAccidentDetail(String oid) {
		return listPolicyDao.queryFanhuaAccidentDetail(oid);
	}
	@Override
	public ListPolicy queryZhonganAccidentDetail(String oid) {
		return listPolicyDao.queryZhonganAccidentDetail(oid);
	}
	@Override
	public ListPolicy queryFanhuaCarDetail(String oid) {
		return listPolicyDao.queryFanhuaCarDetail(oid);
	}
	@Override
	public ListPolicy queryZhonganCarDetail(String oid) {
		return listPolicyDao.queryZhonganCarDetail(oid);
	}
	@Override
	public List<ListPolicy> queryList(ListPolicySearch search,PagerModel pagerModel){
		return listPolicyDao.queryList(search, pagerModel);
	}
	
	@Override
	public boolean setFanhuaCarPolicy(FanhuaCar fanhuaCar){
		ListPolicy listPolicy = new ListPolicy();
		listPolicy.setId(CommonUtils.getKeyId());
		listPolicy.setCustomerId(fanhuaCar.getCustomerId());
		listPolicy.setSigns(PolicySignsState.fanhuaCar);
		listPolicy.setProductName("品牌车险");
		listPolicy.setStartDate(fanhuaCar.getBizInsureInfoStartDate());
		listPolicy.setEndDate(fanhuaCar.getBizInsureInfoEndDate());
		listPolicy.setCreateDate(fanhuaCar.getCreateDate());
		listPolicy.setOid(fanhuaCar.getTaskId());
		listPolicy.setDeleteMark("1");
		listPolicy.setModelStatus(ModelStatus.add);
		if (!applyChange(listPolicy)) {
			Log4j2LogFactory.get().error("listPolicy save error");
		}
		
		// 任务临时注释
		/*
		 * //判断每周每月 然后给上级发金豆 updataGoldenBean(listPolicy.getCustomerId());
		 */
		return true;
	}

	@Override
	public boolean setFanhuaAccidentPolicy(FanhuaAccident fanhuaAccident) {
		ListPolicy listPolicy = new ListPolicy();
		listPolicy.setId(CommonUtils.getKeyId());
		listPolicy.setCustomerId(fanhuaAccident.getCustomerId());
		listPolicy.setSigns(PolicySignsState.fanhuaAccident);
		listPolicy.setProductName(fanhuaAccident.getProductName());
		listPolicy.setStartDate(fanhuaAccident.getStartTime());
		listPolicy.setEndDate(fanhuaAccident.getEndTime());
		listPolicy.setCreateDate(fanhuaAccident.getSubmitTime());

		String policyNo = fanhuaAccident.getPolicyNo();
		if (!StringUtils.isEmpty(policyNo)) {
			listPolicy.setOid(policyNo);
			
		}
		listPolicy.setDeleteMark("1");
		listPolicy.setModelStatus(ModelStatus.add);
		if (!applyChange(listPolicy)) {
			Log4j2LogFactory.get().error("listPolicy fanhuaAccident save error");
		}
		
		//任务临时注释
		/*//判断每周每月 然后给上级发金豆
		updataGoldenBean(listPolicy.getCustomerId());*/
		
		return true;
	}
	
	@Override
	public boolean setZhonganAccidentPolicy(ZhonganAccident zhonganAccident) {
		ListPolicy listPolicy = new ListPolicy();
		listPolicy.setId(CommonUtils.getKeyId());
		listPolicy.setCustomerId(zhonganAccident.getUserId());
		listPolicy.setSigns(PolicySignsState.zhonganAccident);
		listPolicy.setProductName(zhonganAccident.getProductName());
		listPolicy.setStartDate(zhonganAccident.getEffectiveDate());
		listPolicy.setEndDate(zhonganAccident.getExpiryDate());
		listPolicy.setCreateDate(zhonganAccident.getRequestDate());

		String policyNo = zhonganAccident.getPolicyNo();
		if (!StringUtils.isEmpty(policyNo)) {
			listPolicy.setOid(policyNo);
			
		}
		listPolicy.setDeleteMark("1");
		listPolicy.setModelStatus(ModelStatus.add);
		if (!applyChange(listPolicy)) {
			Log4j2LogFactory.get().error("listPolicy zhonganAccident save error");
		}
		
		//任务临时注释
		/*//判断每周每月 然后给上级发金豆
		updataGoldenBean(listPolicy.getCustomerId());*/
		
		return true;
	}
	
	@Override
	public boolean setZhonganCarPolicy(ZhonganCar zhonganCar) {
		ListPolicy listPolicy = new ListPolicy();
		listPolicy.setId(CommonUtils.getKeyId());
		listPolicy.setCustomerId(zhonganCar.getUserId());
		listPolicy.setSigns(PolicySignsState.zhonganCar);
		listPolicy.setProductName(zhonganCar.getProductName());
		try {
			listPolicy.setStartDate(zhonganCar.getEffectiveDate());
			listPolicy.setEndDate(zhonganCar.getExpiryDate());
		} catch (Exception e) {
			Log4j2LogFactory.get().error("listPolicy effectiveDate save error");
		}
		listPolicy.setCreateDate(zhonganCar.getRequestDate());
		listPolicy.setOid(zhonganCar.getPolicyNo());
		listPolicy.setDeleteMark("1");
		listPolicy.setModelStatus(ModelStatus.add);
		if (!applyChange(listPolicy)) {
			Log4j2LogFactory.get().error("listPolicy zhonganCar save error");
		}
		//任务临时注释
		/*//判断每周每月 然后给上级发金豆
		updataGoldenBean(listPolicy.getCustomerId());*/

		return true;
	}
	
	public boolean updataGoldenBean(String customerId){
		Customer customer = customerService.queryModel(customerId);
		String level = customer.getLevel();

		if("visitor".equals(level)){
			String refereeId  = customer.getRefereeId();
			List<Customer> customerList = customerService.queryNextVisitors(refereeId);
			int weekPolicyCount =0;
			int monthPolicyCount =0;
			WithdrawParam weekPolicyParam = withdrawParamService.queryModelByHelpCode("weekPolicy");
			WithdrawParam monthPolicyParam = withdrawParamService.queryModelByHelpCode("monthPolicy");
			for(Customer customer1:customerList){
				String custId = customer1.getId();
				int weekPolicy = Integer.valueOf(queryCountWeekPolicys(custId));
				if(weekPolicy>=weekPolicyParam.getParam()){
					weekPolicyCount = weekPolicyCount+1;
				}
				int monthPolicy = Integer.valueOf(queryCountMonthPolicys(customerId)); 
				if(monthPolicy>=monthPolicyParam.getParam()){
					monthPolicyCount= monthPolicyCount+1;
				}
			}
			if(weekPolicyCount==1){
				if(!goldenBeanDetailService.addGoldenBeanDetail(TaskId.weekVistorOrder, 500, GoldenBeanType.weekVistorOrder, refereeId)){
					return true;
				}
			}
			if(monthPolicyCount==3){
				if(!goldenBeanDetailService.addGoldenBeanDetail(TaskId.monthVisitorOrder, 2000, GoldenBeanType.monthVisitorOrder, refereeId)){
					return true;
				}
			}
		}
		return false;
	}
	@Override
	public int queryCountWeekPolicys(String customerId) {
		return listPolicyDao.queryCountWeekPolicys(customerId);
	}
	@Override
	public int queryCountMonthPolicys(String customerId) {
		return listPolicyDao.queryCountMonthPolicys(customerId);
	}
	@Override
	public Boolean queryCountWeekPolicy(String customerId) {
		List<Customer> customers = customerService.queryCurrentChildCustomer(customerId);
		int weekPolicy = 0;
		for(Customer customer : customers){
			weekPolicy += Integer.valueOf(queryCountWeekPolicys(customer.getId()));
		}
		if(weekPolicy>=1){
			return true;
		}
		return false;
	}
	@Override
	public Boolean queryCountMonthPolicy(String customerId) {
		List<Customer> customers = customerService.queryCurrentChildCustomer(customerId);
		int monthPolicy = 0;
		for(Customer customer : customers){
			monthPolicy +=Integer.valueOf(queryCountMonthPolicys(customer.getId()));;
		}
		if(monthPolicy>=3){
			return true;
		}
		return false;
	}
}